from django.dispatch import Signal
from django.dispatch import receiver
from backend.cews.models import RepairOrderTask
from backend.eform.models import Eform
from backend.cews.eform_single import eform_update_user_singlel
from django.db import transaction

eform_done_signal = Signal()
eform_save_signal = Signal()
eform_verify_signal = Signal()
eform_endorse_signal = Signal()


def update_eform(instance: RepairOrderTask):
    eform_obj = Eform.objects.get(id=instance.eform_id)
    eform_obj.verifiedby_user_id = instance.verify_user_id
    eform_obj.endorsedby_user_id = instance.endorse_user_id
    if eform_obj.workOrder == None:
        eform_obj.workOrder = {}
    with transaction.atomic():
        eform_obj.save()

@receiver(eform_update_user_singlel)
def update_eform_check_user_singlel(sender,instance:RepairOrderTask,**kwargs):
    print("update_eform_check_user_singlel")
    update_eform(instance)
    